<template>
  <div class="register-container">
    <!-- 梦幻动态背景 -->
    <div class="dream-background">
      <!-- 渐变光晕 -->
      <div class="gradient-orb orb1"></div>
      <div class="gradient-orb orb2"></div>
      <div class="gradient-orb orb3"></div>

      <!-- 流星效果 -->
      <div class="meteor-shower">
        <div
          v-for="n in 8"
          :key="n"
          class="meteor"
          :style="{
            '--delay': `${Math.random() * 2}s`,
            '--duration': `${Math.random() * 2 + 1}s`,
            '--top': `${Math.random() * 40}%`,
            '--left': `${Math.random() * 100}%`,
          }"
        ></div>
      </div>

      <!-- 动态粒子 -->
      <div class="particles-container">
        <div
          v-for="n in 50"
          :key="n"
          class="particle"
          :style="{
            '--delay': `${Math.random() * 15}s`,
            '--size': `${Math.random() * 4 + 1}px`,
            '--start-x': `${Math.random() * 100}%`,
            '--start-y': `${Math.random() * 100}%`,
            '--color': `hsl(${Math.random() * 60 + 20}, 80%, 70%)`,
          }"
        ></div>
      </div>
    </div>

    <div class="register-box">
      <div class="welcome-box">
        <h3>欢迎加入</h3>
        <p>智绘灵图库 - 企业级智能协同云图库</p>
        <a-button class="login-button" @click="goToLogin"> 返回登录 </a-button>
      </div>
      <div class="register-form">
        <h2>用户注册</h2>
        <a-form :model="formState" name="basic" autocomplete="off" @finish="handleSubmit">
          <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号' }]">
            <a-input v-model:value="formState.userAccount" placeholder="请输入账号" />
          </a-form-item>
          <a-form-item
            name="userPassword"
            :rules="[
              { required: true, message: '请输入密码' },
              { min: 8, message: '密码长度不能小于 8 位' },
            ]"
          >
            <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码" />
          </a-form-item>
          <a-form-item
            name="checkPassword"
            :rules="[
              { required: true, message: '请输入确认密码' },
              { min: 8, message: '确认密码长度不能小于 8 位' },
            ]"
          >
            <a-input-password
              v-model:value="formState.checkPassword"
              placeholder="请输入确认密码"
            />
          </a-form-item>
          <a-form-item>
            <a-button type="primary" html-type="submit" class="submit-button"> 立即注册 </a-button>
          </a-form-item>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { userRegisterUsingPost } from '@/api/userController'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const formState = reactive<API.UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

const handleSubmit = async (values: any) => {
  if (values.userPassword !== values.checkPassword) {
    message.error('两次输入的密码不一致')
    return
  }
  const res = await userRegisterUsingPost(values)
  if (res.data.code === 0 && res.data.data) {
    message.success('注册成功')
    router.replace('/user/login')
  } else {
    message.error('注册失败，' + res.data.message)
  }
}

const goToLogin = () => {
  router.push('/user/login')
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
  padding: 20px;
}

/* 梦幻背景 - 与登录页相同的样式 */
.dream-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #fff6f0, #ffe4d4);
  overflow: hidden;
}

.register-box {
  position: relative;
  display: flex;
  flex-direction: row-reverse; /* 反转方向，让表单在右边 */
  border-radius: 20px;
  overflow: hidden;
  width: 800px;
  min-height: 500px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(31, 38, 135, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: registerBoxFadeIn 0.8s ease-out;
}

.welcome-box {
  flex: 1;
  background: linear-gradient(135deg, #ff9f43, #ff7f50);
  padding: 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  text-align: center;
}

.register-form {
  flex: 1;
  padding: 50px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
}

h2 {
  margin-bottom: 24px;
  color: #333;
  font-size: 28px;
  text-align: center;
}

h3 {
  font-size: 32px;
  margin-bottom: 16px;
  color: white;
}

.welcome-box p {
  font-size: 16px;
  margin-bottom: 32px;
  opacity: 0.9;
}

.login-button {
  background: transparent;
  border: 2px solid white;
  color: white;
  height: 40px;
  width: 140px;
  font-size: 16px;
  border-radius: 20px;
  transition: all 0.3s;
}

.login-button:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
}

.submit-button {
  width: 100%;
  height: 40px;
  border-radius: 20px;
  background: linear-gradient(135deg, #ff9f43, #ff7f50);
  border: none;
  font-size: 16px;
  transition: all 0.3s ease;
}

.submit-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(255, 159, 67, 0.3);
}

/* 输入框样式 */
:deep(.ant-input),
:deep(.ant-input-password) {
  height: 40px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255, 159, 67, 0.2);
  transition: all 0.3s ease;
}

:deep(.ant-input:hover),
:deep(.ant-input-password:hover) {
  border-color: #ff9f43;
  box-shadow: 0 0 0 2px rgba(255, 159, 67, 0.1);
}

:deep(.ant-input:focus),
:deep(.ant-input-password-focused) {
  border-color: #ff9f43;
  box-shadow: 0 0 0 2px rgba(255, 159, 67, 0.2);
}

/* 动画效果 - 复用登录页的动画 */
@keyframes registerBoxFadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .register-box {
    flex-direction: column;
    width: 100%;
    max-width: 500px;
  }

  .welcome-box,
  .register-form {
    width: 100%;
    padding: 30px;
  }

  .welcome-box {
    padding: 40px 30px;
  }
}

/* 复用登录页的其他动画样式（流星、粒子等） */
.gradient-orb,
.meteor-shower,
.particles-container {
  /* 复用登录页的相关样式 */
}
</style>
